package cn.gbase.jiangsu.data.transfer.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.Community;
import cn.gbase.jiangsu.data.transfer.bean.CompanyUser;
import cn.gbase.jiangsu.data.transfer.bean.SystemUser;
import cn.gbase.jiangsu.data.transfer.common.Constants;

@IocBean(args = { "refer:dao" })
public class CommunityService extends IdEntityService<Community> {

	public CommunityService() {
		super();
	}

	public CommunityService(Dao dao) {
		super(dao);
	}

	/**
	 * 根据ID取得社区.
	 * 
	 * @param CommunityId
	 * @return
	 */
	public Community getCommunity(int CommunityId) {
		return this.dao().fetch(Community.class, CommunityId);
	}

	/**
	 * 取得社区列表.
	 * 
	 * @param pageNumber
	 * @param name
	 * @return
	 */
	public QueryResult getCommunityList(int pageNumber, String keyword,
			String contact) {

		if (pageNumber <= 0) {
			pageNumber = 1;
		}

		// 拼接条件
		SqlExpressionGroup group = Cnd.exps("1", "=", 1);
		if (keyword != null && keyword.length() > 0) {
			group = group.and("communityName", "like", "%" + keyword + "%");
		}
		if (contact != null && contact.length() > 0) {
			group = group.and("address", "like", "%" + contact + "%");
		}

		// 最大页数
		Pager pager = this.dao().createPager(pageNumber,
				Constants.LIST_ONE_PAGE_SIZE);

		// 查询记录总数
		int recordCount = this.dao().count(Community.class, Cnd.where(group));
		pager.setRecordCount(recordCount);

		// 没数据，直接返回
		if (recordCount == 0) {
			return new QueryResult(new ArrayList<Community>(0), pager);
		}

		// 计算页数
		int maxPageNumber = 0;
		if (recordCount % Constants.LIST_ONE_PAGE_SIZE == 0) {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE;
		} else {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE + 1;
		}
		if (pageNumber > maxPageNumber) {
			pager.setPageNumber(maxPageNumber);
		}

		// 查询
		List<Community> resultList = this.dao().query(Community.class,
				Cnd.where(group).desc("id"), pager); // 当前页面显示记录

		// 返回
		return new QueryResult(resultList, pager);
	}

	/**
	 * 取得社区列表.
	 * 
	 * @return
	 */
	public List<Community> getCommunityList() {
		return this.dao().query(Community.class,
				Cnd.orderBy().asc("communityCode"));
	}

	/**
	 * 添加或更新社区.
	 * 
	 * @param Community
	 * @return
	 */
	public boolean addOrUpdCommunity(Community Community) {
		if (Community.getId() > 0) {
			return updateCommunity(Community);
		}
		return addCommunity(Community);
	}

	/**
	 * 添加社区.
	 * 
	 * @param Community
	 * @return
	 */
	public boolean addCommunity(Community Community) {

		// 添加社区
		this.dao().insert(Community);
		return true;
	}

	/**
	 * 更新社区.
	 * 
	 * @param Community
	 * @return
	 */
	public boolean updateCommunity(Community Community) {
		this.dao().update(Community);
		return true;
	}

	/**
	 * 删除社区.
	 * 
	 * @param CommunityId
	 */
	public void deleteCommunity(int CommunityId) {
		this.dao().delete(Community.class, CommunityId); 
		
		//其它关联信息，都要逻辑删除
	}

}
